package com.ruoyi.farmer.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.farmer.domain.TraceUserCrops;
import com.ruoyi.farmer.service.TraceUserCropsService;
import com.ruoyi.farmer.mapper.TraceUserCropsMapper;
import com.ruoyi.system.api.RemoteDeptService;
import com.ruoyi.system.api.RemoteLogisticsService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.TraceTransport;
import com.ruoyi.system.api.domain.vo.CropsVo;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author
 * @description 针对表【trace_user_crops(农作物管理)】的数据库操作Service实现
 * @createDate 2023-05-26 10:29:56
 */
@Service
@Slf4j
public class TraceUserCropsServiceImpl extends ServiceImpl<TraceUserCropsMapper, TraceUserCrops>
        implements TraceUserCropsService {

    @Autowired
    private TraceUserCropsMapper traceUserCropsMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RemoteDeptService remoteDeptService;


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RemoteLogisticsService remoteLogisticsService;
    /**
     *  redis前缀 用于防止kafka重复消费
     */
    private static final String PREVENT_RC ="traceTransport:";

    @Override
    public int saveCrops(TraceUserCrops traceCrops) {
        return traceUserCropsMapper.insert(traceCrops);
    }

    @Override
    public List<TraceUserCrops> getCropsByUsername(String username) {
        return traceUserCropsMapper.getCropsByUsername(username);
    }

    /**
     * 根据部门获取所有司机
     *
     * @param deptId
     * @return
     */
    @Override
    public List<SysUser> getAllDriverByDeptId(Integer deptId) {
        if (redisTemplate.hasKey("getUserByDeptId::" + deptId)){
            return  (List<SysUser>) redisTemplate.opsForValue().get("getUserByDeptId::" + deptId);
        }
        return remoteUserService.getUserByDeptId(deptId, SecurityConstants.INNER).getData();
    }

    /**
     * 通过农作物id修改农作物状态 并远程调用物流服务进行通知运输
     * @param traceTransport
     * @return
     */
    @Override
    public int addTransport(TraceTransport traceTransport) {
        int i = traceUserCropsMapper.updateByCropsId(traceTransport.getCropsId());
        R<Integer> addTransport = remoteLogisticsService.addTransport(traceTransport, SecurityConstants.INNER);
        return addTransport.getData();
    }

    /**
     * 根据部门id获取工程列表
     *
     * @param deptId
     * @return
     */
    @Override
    public List<SysDept> getFactoryByDeptId(Integer deptId) {
        return remoteDeptService.getFactoryByDeptId(deptId, SecurityConstants.INNER).getData();
    }

    /**
     * 修改农产品加工状态
     *
     * @param cropsVo
     * @return
     */
    @Override
    public int updateMachingStatus(CropsVo cropsVo) {
        return traceUserCropsMapper.updateMachingStatus(cropsVo);
    }

    /**
     * 修改农产品填写状态
     *
     * @param cropsId
     * @return
     */
    @Override
    public int updateProductWriteStatus(String cropsId) {
        return traceUserCropsMapper.updateProductWriteStatus(cropsId);
    }

    /**
     * 农产品出库
     *
     * @param cropsId
     * @return
     */
    @Override
    public int productOutFactory(String cropsId) {
        return traceUserCropsMapper.productOutFactory(cropsId);
    }
}




